import complex_query from '@dfeidao/atom-nodejs/db/complex-query';
import query from '@dfeidao/atom-nodejs/db/query';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { productid, systemid } from '../../atom/config';

interface Message {
	no: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:d:\a飞道考核\kh\src\imple-records\zj-000005\s003,action_id:' + action_id);

	// const q = query(action_id, session_id, systemid);
	// q.prepare('hazard_source',
	// 	['hidden_name', 'hidden_source'],
	// 	{ productid }, 200, 1, [], []);

	// const res = await q.exec();

	const q2 = complex_query(action_id, session_id, systemid);
	// .add_field('instance_check_results', 'hidden_source', 'hidden_source')
	// .add_field('instance_check_results', 'hidden_source_feature_no', 'hidden_source_feature_no')
	// .add_field('instance_check_results', 'case_inspect_result_no', 'case_inspect_result_no')
	// .add_field('instance_check_results', 'lnspection_result', 'lnspection_result')


	// .where_eq('instance_check_results', 'check_item_no', message.no)
	// .where_eq_tb('instance_check_results', 'hidden_source', 'hazard_source', 'hidden_source')
	// .where_eq('instance_check_results', 'check_item_no', message.no)
	// // .outer_right_join('instance_check_results', 'hazard_source', ['hidden_source', 'hidden_source'])
	// .where_eq('instance_check_results', 'productid', productid)
	const res = await q2
		.add_field('instance_check_results', 'hidden_source', 'hidden_source')
		.add_field('instance_check_results', 'hidden_source_feature_no', 'hidden_source_feature_no')
		.add_field('instance_check_results', 'case_inspect_result_no', 'case_inspect_result_no')
		.add_field('instance_check_results', 'lnspection_result', 'lnspection_result')
		.add_field('hazard_source', 'hidden_name', 'hidden_name')
		.add_field('hazard_source_feature', 'hidden_source_feature_name', 'hidden_source_feature_name')
		.add_field('hazard_source_feature', 'control_data', 'control_data')
		.where_eq('instance_check_results', 'check_item_no', message.no)
		.where_eq('instance_check_results', 'productid', productid)
		.outer_left_join('instance_check_results', 'hazard_source', ['hidden_source', 'hidden_source'])
		.outer_left_join('instance_check_results', 'hazard_source_feature', ['hidden_source_feature_no', 'hidden_source_feature_no'])
		.exec();


	log('Service end path:d:\a飞道考核\kh\src\imple-records\zj-000005\s003,action_id:' + action_id);
	return {
		data: res
	};
}
